import axios from 'axios'
import base from './base'

const http = axios.create({
  baseURL: base.url,
  timeout: 5000
})

// 请求拦截器
http.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    return config
  },
  error => {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 响应拦截器
http.interceptors.response.use(
  response => {
    // 对响应数据做些什么
    return response.data
  },
  error => {
    // 对响应错误做些什么
    return Promise.reject(error)
  }
)


// GET请求
export const get = (url, params = {}) => {
  return http.get(url, { params })
}

// POST请求
export const post = (url, data = {}) => {
  return http.post(url, data)
}

// PUT请求
export const put = (url, data = {}) => {
  return http.put(url, data)
}

// DELETE请求
export const del = (url, params = {}) => {
  return http.delete(url, { params })
}

export default http